package com.qf.fc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fc.Param.LikeAdd;
import com.qf.fc.dao.CommentLikeDao;
import com.qf.fc.dao.MoodLikeDao;
import com.qf.fc.entity.MoodLike;
import com.qf.fc.service.MoodLikeService;
import com.qf.fc.vo.R;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 动态点赞表(MoodLike)表服务实现类
 *
 * @author makejava
 * @since 2024-09-23 23:34:47
 */
@Service("moodLikeService")
@RequiredArgsConstructor
public class MoodLikeServiceImpl implements MoodLikeService {
    private final MoodLikeDao dao;

    @Override
    public R addLike(LikeAdd add) {
        //1.验证
        if (add==null && add.getLid()>0){
            return R.fail("亲，请检查参数信息");
        }
        int a=0;
        //查询之前是否点过赞
        LambdaQueryWrapper<MoodLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MoodLike::getUid,add.getUid())
                .eq(MoodLike::getMid,add.getLid());
        MoodLike moodLike = dao.selectOne(wrapper);
        if (moodLike==null){
            //说明之前该用户没有点过赞，本次新增
            moodLike=new MoodLike();
            moodLike.setMid(add.getLid());
            moodLike.setUid(add.getUid());
            moodLike.setCreateTime(new Date());
            a=dao.insert(moodLike); 
        } else{
            //之前点过本次取消
            a=dao.deleteById(moodLike.getId());
        }
        if (a>0){
            //成功
            return R.ok("操作成功");
        }
        return R.fail("亲，网络故障，请检查。");
    }

    /**
     * 查询我的点赞
     */
    @Override
    public R queryMy(Integer uid) {
        return R.ok(dao.selectLikes(uid));
    }
    /**
     * 检查是否点赞
     */

    @Override
    public R checkLike(Integer uid, Integer mid) {
        return R.ok(dao.checkLike(uid, mid));
    }
}

